/**
 * @author : 乔磊磊
 * @ide    : WebStorm
 * @date   : 2025/2/22 星期六 10:00
 * @desc   :
 */

export class DateUtil {

    /**
     * 根据传入的时间(毫秒为单位)判断是否为新的一天
     */
    public static isNewDay(time: number): boolean {
        let date = new Date(time);
        let today = new Date();
        if (date < today) return false;
        return date.getFullYear() !== today.getFullYear() || date.getMonth() !== today.getMonth() || date.getDate() !== today.getDate();
    }

    /**
     * 根据传入的时间起点和终点(毫秒为单位)，判断当前时间是否在有效期内
     */
    public static isInValidPeriod(startTime: number, endTime: number): boolean {
        let currentTime = Date.now();
        return !(currentTime < startTime || currentTime > endTime);
    }

    /**
     * 根据传入的时间起点和终点(毫秒为单位)，返回间隔的天数
     */
    public static getIntervalDays(startTime: number, endTime: number): number {
        return Math.floor((endTime - startTime) / (1000 * 60 * 60 * 24));
    }

    /**
     * 根据剩余秒数格式化剩余秒数 返回 HH:MM:SS
     */
    public static formatRemainingTime(remainingSeconds: number): string {
        let timeStr: string = ""
        let hours = Math.floor(remainingSeconds / (60 * 60));
        let minutes = Math.floor((remainingSeconds % (60 * 60)) / 60);
        let seconds = remainingSeconds % 60;
        if (hours > 0) {
            timeStr += hours > 9 ? hours.toString() : "0" + hours
            timeStr += ":"
        }

        if (minutes > 0) {
            timeStr += minutes > 9 ? minutes.toString() : "0" + minutes
            timeStr += ":"
        } else {
            timeStr += "00:"
        }

        timeStr += seconds > 9 ? seconds.toString() : "0" + seconds

        return timeStr
    }
}
